package cn.lutu.lutu.service.impl;

import cn.lutu.lutu.pojo.dto.UserRegDTO;
import cn.lutu.lutu.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

@Slf4j
@Service
public class EmailServiceImpl implements EmailService {


    public String sendAllEmail(String emailAddress, String subject, String text) {
        return null;
    }



    public SimpleMailMessage createCodeEmail(String email,Integer emailCode) {
        //创建邮件对象
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置收件人
        mailMessage.setTo(email);
        //设置发件人
        mailMessage.setFrom("475186415@qq.com");
        //设置标题
        mailMessage.setSubject("鹿途网用户注册邮箱验证码！");
        //设置正文
        String nickName=email.substring(0,email.lastIndexOf("@"));
        mailMessage.setText(EmailCodeText(nickName,emailCode));
        //发送邮件
        return mailMessage;

    }


    public String EmailCodeText(String nickName,Integer emailCode) {

        return "亲爱的"+nickName+"用户，您正在注册鹿途网账户，您的验证码为: "+emailCode+" !请妥善保管此验证码，切勿相信他人，" +
                "随意告诉他人！感谢您的支持和信任！祝您生活愉快！";
    }

    public Integer randomEmailCode() {
        StringBuilder builder=new StringBuilder();
        Random rand= new Random();
        for(int i=0;i<6;i++){
            int m=rand.nextInt(10);
            builder.append(m);
        }
        ;
        log.info("邮箱验证码为："+Integer.parseInt(builder.toString()));
        return Integer.parseInt(builder.toString());
    }


}
